import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    private Socket socket=null;
    public TcpEchoClient(String serverIP,int serverPort) throws IOException {
         socket = new Socket(serverIP, serverPort);
    }
    public void start(){
        // 1.从控制台读取用户输入的内容
        // 2.把字符串作为请求发送给服务器
        // 3.从服务器读取响应
        // 4.把响应显示出来
        Scanner scanner = new Scanner(System.in);
        try (InputStream inputStream = socket.getInputStream();
             OutputStream outputStream = socket.getOutputStream()){
            PrintWriter writer = new PrintWriter(outputStream);
            Scanner scannerNetwork = new Scanner(inputStream);
            while(true){
                System.out.print("-> ");
                String request = scanner.next();
                writer.println(request);
                writer.flush();
               String response = scannerNetwork.next();
                System.out.println(response);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client = new TcpEchoClient("127.0.0.1",9090);
        client.start();
    }

}
